'use strict';
import * as types from '../Commom/ActionConst';
import APIService from '../Commom/APIService';
import Global from '../Commom/GlobalConst';
import UserAPIService from '../Commom/UserAPIService';
export let UserFromStorage = () => {
    return (dispatch) => {
        APIService.loadStorageUserInfo().then((ret)=> {
            dispatch({
                type: types.kUserFromStorage,
                phone: ret.phone,
                user_name: ret.user_name,
                head_picture: ret.head_picture,
                isLoggedIn: true,
                isLoggedOut: false
            });
        }).catch((err)=> {
            dispatch({
                type: types.kUserFromStorage,
                phone: '',
                user_name: '',
                head_picture: '',
                isLoggedIn: false,
                isLoggedOut: false
            });
        });
    }
};
export let thirdLogin = (openid, user_name, head_picture, registered_id) => {
    return (dispatch) => {
        dispatch({'type': types.kUserLogin});
        UserAPIService.thirdLogin(openid, user_name, head_picture, registered_id).then((ret)=> {
            dispatch({type: types.kUserLoginReceived, result: ret});
        }).catch((err)=> {
            dispatch({type: types.kUserLoginReceived, result: null});
        });
    }
}
export let userInfo = (token, registered_id) => {
    return (dispatch) => {
        dispatch({'type': types.kUserInfo});
        UserAPIService.userInfo(token, registered_id).then((ret)=> {
            dispatch({type: types.kUserInfoReceived, result: ret});
        }).catch((err)=> {
            dispatch({type: types.kUserInfoReceived, result: null});
        });
    }
}
export let getCode = (phone) => {
    return (dispatch) => {
        UserAPIService.getCode(phone).then((ret)=> {
            dispatch({type: types.kPhoneCodeReceived});
        }).catch((err)=> {
            dispatch({type: types.kPhoneCodeError});
        });
    }
}
export let getForgotPasswordCode = (phone) => {
    return (dispatch) => {
        UserAPIService.getCode(phone).then((ret)=> {
            dispatch({type: types.kPhoneForgotPasswordCodeReceived});
        }).catch((err)=> {
            dispatch({type: types.kPhoneForgotPasswordCodeError});
        });
    }
}
export let getChangePassWordCode = (phone) => {
    return (dispatch) => {
        UserAPIService.getCode(phone).then((ret)=> {
            dispatch({type: types.kPhoneChangePassWordCodeReceived});
        }).catch((err)=> {
            dispatch({type: types.kPhoneChangePassWordCodeError});
        });
    }
}
export function SMSCodeClearStatus() {
    return dispatch => {
        dispatch({'type': types.kPhoneCodeClearStatus});
    }
};
export let changePassword = (token, registered_id, phone, password, code) => {
    return (dispatch) => {
        dispatch({'type': types.kUserChangePassWord});
        UserAPIService.changePassword(token, registered_id, phone, password, code).then((ret)=> {
            dispatch({type: types.kUserChangePassWordReceived, result: ret});
        }).catch((err)=> {
            dispatch({type: types.kUserChangePassWordReceived, result: null});
        });
    }
}
export let forget = (phone, password, code) => {
    return (dispatch) => {
        dispatch({'type': types.kUserChangePassWord});
        UserAPIService.forget(phone, password, code).then((ret)=> {
            dispatch({type: types.kUserChangePassWordReceived, result: ret});
        }).catch((err)=> {
            dispatch({type: types.kUserChangePassWordReceived, result: null});
        });
    }
}

export let LoginAction = (username, password) => {
    return (dispatch) => {
        dispatch({'type': types.kUserLogin});
        let curuser = {
            username: username,
            password: password,
            head: require('../../Image/icon_not_logged.png'),
            avatarUrl: '../../Image/icon_not_logged.png',
            nickname: username,
        };
        setTimeout(()=> {
            storage.save({
                key: Global.Const.storagekeyLogin,
                rawData: curuser
            });
            dispatch({
                type: types.kUserLoginReceived,
                user: curuser,
                isLoggedIn: true,
                isLoggedOut: false,
                isThirdLogin: false,
            });
        }, 200);
    }
};
export let ThirdLoginAction = (username, password, headUrl) => {
    return (dispatch) => {
        dispatch({'type': types.kUserThirdLogin});
        let curuser = {
            username: '',
            password: password,
            head: {uri: headUrl},
            avatarUrl: headUrl,
            nickname: username,
        };
        setTimeout(()=> {
            storage.save({
                key: Global.Const.storagekeyLogin,
                rawData: curuser
            });
            dispatch({
                type: types.kUserThirdLoginReceived,
                user: curuser,
                isLoggedIn: true,
                isLoggedOut: false,
                isThirdLogin: true,
            });
        }, 200);
    }
};
export let RegisterAction = (username, password, avatarSource, avatarUrl) => {
    return (dispatch) => {
        dispatch({'type': types.kUserLogin});
        let curuser = {
            username: '',
            password: password,
            avatarUrl: avatarUrl,
            head: avatarSource,
            nickname: username,
        };
        setTimeout(()=> {
            storage.save({
                key: Global.Const.storagekeyLogin,
                rawData: curuser
            });
            dispatch({
                type: types.kUserLoginReceived,
                user: curuser,
                isLoggedIn: true,
                isLoggedOut: false,
                isThirdLogin: false,
            });
        }, 200);
    }
};
export let ChangeNickNameAction = (userInfo) => {
    return (dispatch) => {
        dispatch({'type': types.kUserEditData});

        setTimeout(()=> {
            storage.save({
                key: Global.Const.storagekeyLogin,
                rawData: userInfo
            });
            dispatch({
                type: types.kUserEditDataReceived,
                user: userInfo,
            });
        }, 200);
    }
};
export let ChangePassWordAction = (userInfo) => {
    return (dispatch) => {
        dispatch({'type': types.kUserEditData});

        setTimeout(()=> {
            storage.save({
                key: Global.Const.storagekeyLogin,
                rawData: userInfo
            });
            dispatch({
                type: types.kUserEditDataReceived,
                user: userInfo,
            });
        }, 200);
    }
};
export let ChangeHeadImageAction = (userInfo) => {
    return (dispatch) => {
        dispatch({'type': types.kUserEditData});

        setTimeout(()=> {
            storage.save({
                key: Global.Const.storagekeyLogin,
                rawData: userInfo
            });
            dispatch({
                type: types.kUserEditDataReceived,
                user: userInfo,
            });
        }, 200);
    };
};
export let UserLogout = () => {
    // let url = urls.kUrlUserLogout;
    return (dispatch) => {
        dispatch({'type': types.kUserLogout});
        setTimeout(()=> {
            storage.remove({
                key: Global.Const.storageUser
            });
            storage.remove({
                key: Global.Const.storagekeySearch
            });
            storage.remove({
                key: Global.Const.storageToken
            });
            Global.Const.token = '';
            dispatch({
                type: types.kUserLogoutReceived,
                user: null,
                isLoggedOut: true,
                isLoggedIn: false,
                isThirdLogin: false,
            });
        }, 200);
    }
};
